package java8Com.Functional;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.BinaryOperator;

public class BinaryOperatorMax {

    static class Developer {
        String name;
        BigDecimal salary;

        public Developer(String name, BigDecimal salary) {
            this.name = name;
            this.salary = salary;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public BigDecimal getSalary() {
            return salary;
        }

        public void setSalary(BigDecimal salary) {
            this.salary = salary;
        }
    }

    public static Developer find(List<Developer> list, BinaryOperator<Developer> binaryOperator){
        Developer result=null;
        for (Developer developer : list) {
            if (result==null){
                result=developer;
            }else {
                binaryOperator.apply(result, developer);
            }
        }

        return result;
    }


    public static void main(String[] args) {

        Developer dev1 = new Developer("jordan", BigDecimal.valueOf(9999));
        Developer dev2 = new Developer("jack", BigDecimal.valueOf(8888));
        Developer dev3 = new Developer("jaden", BigDecimal.valueOf(10000));
        Developer dev4 = new Developer("ali", BigDecimal.valueOf(2000));
        Developer dev5 = new Developer("mkyong", BigDecimal.valueOf(1));

        List<Developer> list = Arrays.asList(dev1, dev2, dev3, dev4, dev5);

        //最大值
        Comparator<Developer> c=(x1,x2) ->x1.getSalary().compareTo(x2.getSalary());

        Comparator<Developer> comparing = Comparator.comparing(Developer::getSalary);
        BinaryOperator<Developer> by = BinaryOperator.maxBy(c);

        list.sort(Comparator.comparing(Developer::getSalary));
        //list.sort((x1,x2) ->x1.getSalary().compareTo(x2.getSalary()));

        list.forEach(x -> System.out.println(x.getName()));

        System.out.println(find(list, by).getName());
    }
}
